/*
* @(#) BinaryTreeOrder.java 2018年02月23日
*
* Copyright 2010 NetEase.com, Inc. All rights reserved.
*/
package com.leo.m1802.d23;

import java.util.LinkedList;
import java.util.Queue;

/**
 *
 * @author xuexiaolei
 * @version 2018年02月23日
 */
public class BinaryTreeOrder {
    class Node{
        int value;
        Node left,right;
        public Node(int value, Node left, Node right) {
            this.value = value;
            this.left = left;
            this.right = right;
        }
    }

    private void visitNode(Node node){
        System.out.print(node.value+" ");
    }

    public void preOrder(Node node){
        if (node!=null){
            visitNode(node);
            preOrder(node.left);
            preOrder(node.right);
        }
    }

    public void midOrder(Node node){
        if (node!=null){
            midOrder(node.left);
            visitNode(node);
            midOrder(node.right);
        }
    }

    public void postOrder(Node node){
        if (node!=null){
            postOrder(node.left);
            postOrder(node.right);
            visitNode(node);
        }
    }

    public void levelOrder(Node node){
        Queue<Node> queue = new LinkedList<Node>();
        queue.add(node);
        while (!queue.isEmpty()){
            Node head = queue.poll();
            if (head!=null) {
                visitNode(head);
                queue.add(head.left);
                queue.add(head.right);
            }
        }
    }
}
